<?php defined('BASEPATH') OR exit('No direct script access allowed');

class users_class extends CI_Model {
	
	/* REGISTER */
	public function insert($post)
	{
		$strUserName = stripslashes(filter_var($post['strUserName'], FILTER_SANITIZE_STRING));
		$strFirstName = stripslashes(filter_var($post['strFirstName'], FILTER_SANITIZE_STRING));
		$strLastName = stripslashes(filter_var($post['strLastName'], FILTER_SANITIZE_STRING));
		$strEmail = stripslashes(filter_var($post['strEmail'], FILTER_SANITIZE_STRING));
		$strPassword =  stripslashes(filter_var($post['strPassword1'], FILTER_SANITIZE_STRING));
		$strRePassword =  stripslashes(filter_var($post['strPassword2'], FILTER_SANITIZE_STRING));
		$strGender =  stripslashes(filter_var($post['strGender'], FILTER_SANITIZE_STRING));
		
		$chk_strUserName = $this->get_user_exist($strUserName);
		$chk_strEmail = $this->get_user_exist($strEmail);
		
		if(empty($strUserName)){
			$msg['status'] = 'put_username';
		}
		else if(empty($strFirstName)){
			$msg['status'] = 'put_firstname';
		}
		else if(empty($strLastName)){
			$msg['status'] = 'put_lastname';
		}
		else if(empty($strEmail)){
			$msg['status'] = 'put_email';
		}
		else if(empty($strPassword)){
			$msg['status'] = 'put_password';
		}
		else if(empty($strRePassword)){
			$msg['status'] = 'retype_password';
		}
		else if(trim($strPassword) != trim($strRePassword))
		{
			$msg['status'] = 'pw_mismatch';
		}
		else if( $chk_strUserName === TRUE )
		{
			$msg['status'] = 'username_exist';
		}
		else if( $chk_strEmail === TRUE )
		{
			$msg['status'] = 'email_exist';
		}
		else if( !valid_email($strEmail) )
		{
			$msg['status'] = 'email_not_valid';
		}
		else
		{
			$valid_code = rand(111,999);
			$data = array(
					'strUserName' => $strUserName,
					'strFirstName' => $strFirstName,
					'strLastName' => $strLastName,
					'strPrimaryEmail' => $strEmail,
					'strPassword' => $this->encrypt->encode($strPassword),
					'strGender' => $strGender,
					'strValidationCode' => $this->encrypt->encode($valid_code),
					'dateJoined' => date('Y-m-d H:i:s')
			);
			$query = $this->db->insert('users', $data);
			if($this->db->_error_number()==0){
				$user_dir = './users/'.$strUserName;
				if(!file_exists($user_dir)){
					mkdir($user_dir, 0755);
				}
				$msg['status'] = 'success';
			}
			else
			{
				$msg['status'] = 'error';
			}
		}
		return $msg;
	}	
	/* LOGIN */
	public function login_auth($post)
	{
		$strUser = stripslashes(filter_var($post['strUserName'], FILTER_SANITIZE_STRING));
		$strPassword = stripslashes(filter_var($post['strPassword'], FILTER_SANITIZE_STRING));
		$strRememberMe = stripslashes(filter_var($post['strRememberMe'], FILTER_SANITIZE_STRING));
		
		$chk_strUserName = $this->get_user_exist($strUser);
		$chk_strEmail = $this->get_user_exist($strUser);
		
		if(empty($strUser))
		{
			$msg['status'] = 'put_username';
		}
		else if(empty($strPassword))
		{
			$msg['status'] = 'put_password';
		}
		else if( $chk_strUserName === FALSE )
		{
			$msg['status'] = 'username_not_exist';
		}
		else if( $chk_strEmail === FALSE )
		{
			$msg['status'] = 'email_not_exist';
		}
		else
		{
			$this->db->where('strUserName',$strUser);
			$this->db->or_where('strPrimaryEmail',$strUser);
			$query = $this->db->get('users')->result_array();
			$query = current($query);
			
			if($query['isUserDeleted'] === 'true')
			{
				$msg['status'] = 'user_deleted'; // Access Denied!
			}
			else
			{
				if($this->encrypt->decode($query['strPassword']) === $strPassword)
				{
					$this->session->set_userdata('intUserID', $this->encrypt->encode($query['intUserID']));
					$this->session->set_userdata('strUserName', $query['strUserName']);
					if($strRememberMe == true)
					{
						$cookie = array(
							'name' => 'strUserName',
							'value' => $query['strUserName'],
							'expire' => time()+86500,
							'path'   => '/',
						);
						$this->input->set_cookie($cookie);
					}
					$msg['username'] = $query['strUserName'];
					$msg['status'] = 'logged_in_success';
				}
				else
				{
					$msg['status'] = 'password_mismatch';
				}
			}
		}
		return $msg;
	}
	
	/* CHECK USER IF EXIST */
	public function get_user_exist($post){
		$strUser = stripslashes(filter_var($post, FILTER_SANITIZE_STRING));
		
		$this->db->where('strUserName',$strUser);
		$this->db->or_where('strPrimaryEmail',$strUser);
		$query = $this->db->get('users');
		$result = $query->num_rows() > 0 ? TRUE : FALSE;
		
		return $result;
	}
	/* Get User Info */
	public function get_my_info()
	{
		$intUserID = $this->encrypt->decode($this->session->userdata('intUserID'));
		$intUserID_cookie = $this->encrypt->decode(get_cookie('intUserID'));
		
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where(array('intUserID'=>$intUserID));
		$this->db->or_where(array('intUserID'=>$intUserID_cookie));
		$query = $this->db->get()->result_array();
		$query = current($query);
		
		if($query['intUserID']==""){
			return 0;
		} else {
			return $query;
		}
	}
	public function get_user_info($username)
	{
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where(array('strUserName'=>$username,'isValidated'=>'true','isUserDeleted'=>'false'));
		$query = $this->db->get()->result_array();
		$query = current($query);
		
		if($query['intUserID']==""){
			return 0;
		} else {
			return $query;
		}
	}
}